package One;
import javax.swing.*;
import javax.swing.plaf.FontUIResource;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.IOException;
import java.net.*;
import java.util.Enumeration;

public class User extends JFrame implements ActionListener, Runnable{



    JTextField outMessage = new JTextField(12);//用于输入发送的数据
    JTextArea inMessage = new JTextArea(12,20);//用于展示接收和发送的消息
    JButton b = new JButton("发送数据");//发送按钮

    InetAddress dstAddr = InetAddress.getByName("localhost");//目标地址 - 服务端地址
    InetAddress srcAddr;//当前客户端的地址
    int dst = 2000;//目标端口号 - 服务端端口号
    int src;//当前客户端的端口号
    String userName;

    //构造方法 - 创建客户端窗口
    public User(String other, String userName,InetAddress srcAddr, int src) throws UnknownHostException
    {
        super("与"+other+"的对话");//定义客户端窗口标题
        this.userName = userName;
        this.srcAddr = srcAddr;
        this.src = src;
        b.addActionListener(this);

        setSize(600,500);
        setVisible(true);

        JPanel p = new JPanel();
        p.add(outMessage);//发送的数据
        p.add(b);//添加发送按钮

        Container con = getContentPane();
        con.add(new JScrollPane(inMessage), BorderLayout.CENTER);//添加消息框
        con.add(p, BorderLayout.NORTH);//添加发送消息的面板

        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        validate();

        Thread thread = new Thread(this);
        thread.start(); //线程负责接收数据

    }

    public String getUserName() {
        return userName;
    }

    public InetAddress getSrcAddr() {
        return srcAddr;
    }

    public int getSrc() {
        return src;
    }

    @Override
    public void actionPerformed(ActionEvent e)
    {
        // 单击按钮触发事件
        byte b[] = (userName+":"+outMessage.getText()).trim().getBytes();//将用户要发送的内容加上用户姓名后整理
        //在用户自己的对话框中显示自己发送的内容
        inMessage.append(userName+"：");
        inMessage.append(outMessage.getText()+"\n");
        outMessage.setText("");//重置发送框中的信息
        //发送数据
        try {
            DatagramPacket dataPack = new DatagramPacket(b,b.length,dstAddr,dst);
            DatagramSocket send = new DatagramSocket();
            send.send(dataPack);
            send.close();
        } catch (SocketException socketException) {
            socketException.printStackTrace();
        } catch (IOException ioException) {
            ioException.printStackTrace();
        }
    }

    @Override
    public void run()
    {
        // 接收数据
        DatagramPacket pack = null;
        DatagramSocket mail = null;
        byte b[] = new byte[8192];
        try
        {
            pack = new DatagramPacket(b,b.length);
            mail = new DatagramSocket(src);
            while(true)
            {
                mail.receive(pack);
                String message = new String(pack.getData(),0,pack.getLength());
                //将接收到的数据添加到对话框中
                inMessage.append(message+"\n");
                inMessage.setCaretPosition(inMessage.getText().length());
            }
        }
        catch(Exception e){}
    }

    //修改swing全局字体 - 在main方法中生成其他GUI图形界面之前调用，可以将字体设置为我们传参的字体
    public static void InitGlobalFont(Font f){
        FontUIResource fontRes = new FontUIResource(f);
        for (Enumeration<Object> keys = UIManager.getDefaults().keys(); keys.hasMoreElements();) {
            Object key = keys.nextElement();
            Object value = UIManager.get(key);
            if (value instanceof FontUIResource) {
                UIManager.put(key, fontRes);
            }
        }
    }

}
